home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 May / PCpro_2006_05.ISO / files / mobile / fma-2.0-stable-setup.exe / {app} / source / uMobileAgentUI.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2004-06-28  |  11.5 KB  |  466 lines

  1. unit uMobileAgentUI;
  2.  
  3. {
  4. *******************************************************************************
  5. * Descriptions: COM/Script Interface implementation
  6. * $Source: /cvsroot/fma/fma/uMobileAgentUI.pas,v $
  7. * $Locker:  $
  8. *
  9. * Todo:
  10. *
  11. * Change Log:
  12. * $Log: uMobileAgentUI.pas,v $
  13. * Revision 1.14  2004/06/28 09:14:06  z_stoichev
  14. * - Added 912934 Add fma.PhoneType method (MHWFO).
  15. * - Added 972252 Add fma.DisconnectTemporary (autoconnect).
  16. *
  17. * Revision 1.13  2004/06/08 19:20:31  lordlarry
  18. * Memory Leak fixed
  19. *
  20. * Revision 1.12  2004/03/20 16:26:17  z_stoichev
  21. * Added fma.ScriptCall(code) COMObject method.
  22. * Added fma.Sleep(msec) COMObject method.
  23. *
  24. * Revision 1.11  2004/02/04 13:01:23  z_stoichev
  25. * Add object methods call support.
  26. *
  27. * Revision 1.10  2004/02/03 16:28:08  z_stoichev
  28. * Added new methods, TxAndWait synced with Form1.
  29. *
  30. * Revision 1.9  2004/01/30 16:37:08  z_stoichev
  31. * Added ObexCut method.
  32. * Made ObexDelete work in silent mode.
  33. *
  34. * Revision 1.8  2004/01/27 15:50:33  z_stoichev
  35. * Changed method definitions (removed 'success' out)
  36. * Added ObexGet and ObexDelete (for files).
  37. *
  38. * Revision 1.7  2004/01/23 08:30:51  z_stoichev
  39. * Fixed Transmit function
  40. *
  41. * Revision 1.6  2003/12/18 12:45:15  z_stoichev
  42. * ObexGetObj and ObexPutObj implemented.
  43. *
  44. * Revision 1.5  2003/11/28 09:38:07  z_stoichev
  45. * Merged with branch-release-1-1 (Fma 0.10.28c)
  46. *
  47. * Revision 1.4.2.1  2003/10/27 07:22:54  z_stoichev
  48. * Build 0.1.0 RC1 Initial Checkin.
  49. *
  50. * Revision 1.4  2003/02/14 11:34:52  crino77
  51. * Added StatusReq in SentMessage
  52. *
  53. * Revision 1.3  2003/01/30 04:15:57  warren00
  54. * Updated with header comments
  55. *
  56. *
  57. *******************************************************************************
  58. }
  59.  
  60. {$WARN SYMBOL_PLATFORM OFF}
  61.  
  62. interface
  63.  
  64. uses
  65.   ComObj, ActiveX, MobileAgent_TLB, StdVcl, Classes, Menus, ExtCtrls;
  66.  
  67. type
  68.   TMobileAgentApp = class(TAutoObject, IMobileAgentApp)
  69.   private
  70.     sl: TStrings;
  71.     tl: TStrings;
  72.     rootMenu: TMenuItem;
  73.   protected
  74.     procedure AddCmd(const label_, event: WideString); safecall;
  75.     procedure Connect; safecall;
  76.     procedure Disconnect; safecall;
  77.     procedure Exit; safecall;
  78.     function Get_KeyPress: WideString; safecall;
  79.     function Get_PopKey: WideString; safecall;
  80.     procedure Debug(const str: WideString); safecall;
  81.     procedure ClearKey; safecall;
  82.     procedure Status(const Str: WideString); safecall;
  83.     procedure Minimize; safecall;
  84.     procedure Restore; safecall;
  85.     procedure Set_KeyInActivityTimeout(Value: Integer); safecall;
  86.     procedure Transmit(const Cmd: WideString); safecall;
  87.     function Get_Connected: Integer; safecall;
  88.     function Get_LookupByNumber(const Number: WideString): WideString;
  89.       safecall;
  90.     function Get_Received: WideString; safecall;
  91.     procedure SentMessage(const Msg, DestNo: WideString; ReqReply,
  92.       Flash, StatusReq: Smallint); safecall;
  93.     procedure ObexPut(const filename: WideString); safecall;
  94.     procedure VoiceAnswer; safecall;
  95.     procedure VoiceCall(const Number: WideString); safecall;
  96.     procedure VoiceHangUp; safecall;
  97.     function ObexGetObj(const filename, objecturl: WideString): HResult;
  98.       safecall;
  99.     function ObexPutObj(const filename, objecturl: WideString): HResult;
  100.       safecall;
  101.     function ObexGet(const filename, objectname: WideString): HResult;
  102.       safecall;
  103.     procedure ObexDelete(const objectname: WideString); safecall;
  104.     function ObexCut(const filename, objectname: WideString): HResult;
  105.       safecall;
  106.     procedure AddTimer(msec: Integer; const event: WideString); safecall;
  107.     procedure DeleteTimer(const event: WideString); safecall;
  108.     procedure EnableKeyMonitor; safecall;
  109.     procedure DisableKeyMonitor; safecall;
  110.     procedure ScriptCall(const Code: WideString); safecall;
  111.     procedure Sleep(msec: Integer); safecall;
  112.     function Get_PhoneType(const Number: WideString): WideString; safecall;
  113.     procedure DisconnectTemporary; safecall;
  114.     { Protected declarations }
  115.   public
  116.     procedure Initialize; override;
  117.     destructor Destroy; override;
  118.     procedure MenuClicked(Sender: TObject);
  119.     procedure TimerEvent(Sender: TObject);
  120.   end;
  121.  
  122. implementation
  123.  
  124. uses ComServ, Unit1, SysUtils, Dialogs, uObex, uSyncPhonebook;
  125.  
  126. var rootCount: integer;
  127.  
  128. procedure TMobileAgentApp.AddCmd(const label_, event: WideString);
  129. var
  130.   menuItem: TMenuItem;
  131. begin
  132.   menuItem := TMenuItem.Create(Form1.MainMenu1);
  133.   menuItem.Caption := label_;
  134.   menuItem.Tag := sl.Count;
  135.   menuItem.OnClick := MenuClicked;
  136.   rootMenu.Add(menuItem);
  137.  
  138.   sl.Add(IntToStr(sl.Count) + '=' + event);
  139. end;
  140.  
  141. procedure TMobileAgentApp.Initialize;
  142. begin
  143.   inherited;
  144.   sl := TStringList.Create;
  145.   tl := TStringList.Create;
  146.  
  147.   with Form1.MainMenu1 do begin
  148.     rootMenu := Items.Find('Tools');
  149.     if rootCount = 0 then rootCount := rootMenu.Count;
  150.   end;
  151. end;
  152.  
  153. procedure TMobileAgentApp.MenuClicked(Sender: TObject);
  154. var
  155.   tag: Integer;
  156.   cmd: String;
  157. begin
  158.   tag := (Sender as TMenuItem).Tag;
  159.   cmd := sl.Values[IntToStr(tag)];
  160.  
  161.   Form1.CallScriptMethod(cmd,[]);
  162. end;
  163.  
  164. procedure TMobileAgentApp.Connect;
  165. begin
  166.   Form1.ActionConnectionConnect.Execute;
  167. end;
  168.  
  169. procedure TMobileAgentApp.Disconnect;
  170. begin
  171.   Form1.ActionConnectionDisconnect.Execute;
  172. end;
  173.  
  174. procedure TMobileAgentApp.Exit;
  175. begin
  176.   Form1.Close;
  177. end;
  178.  
  179. function TMobileAgentApp.Get_KeyPress: WideString;
  180. begin
  181.   Result := Form1.FKeyActivity;
  182. end;
  183.  
  184. function TMobileAgentApp.Get_PopKey: WideString;
  185. begin
  186.   Result := Copy(Form1.FKeyActivity, length(Form1.FKeyActivity), 1);
  187.   Form1.FKeyActivity := Copy(Form1.FKeyActivity, 1, length(Form1.FKeyActivity) - 1);
  188. end;
  189.  
  190. procedure TMobileAgentApp.EnableKeyMonitor;
  191. begin
  192.   Form1.EnableKeyMonitor(True);
  193. end;
  194.  
  195. procedure TMobileAgentApp.Debug(const str: WideString);
  196. begin
  197.   Form1.Debug('[Script] ' + str);
  198. end;
  199.  
  200. procedure TMobileAgentApp.ClearKey;
  201. begin
  202.   Form1.FKeyActivity := '';
  203. end;
  204.  
  205. procedure TMobileAgentApp.Status(const Str: WideString);
  206. begin
  207.   Form1.Status(Str);
  208. end;
  209.  
  210. procedure TMobileAgentApp.Minimize;
  211. begin
  212.   Form1.MinimizeApp;
  213. end;
  214.  
  215. procedure TMobileAgentApp.Restore;
  216. begin
  217.   Form1.ActionWindowRestore.Execute;
  218. end;
  219.  
  220. procedure TMobileAgentApp.Set_KeyInActivityTimeout(Value: Integer);
  221. begin
  222.   Form1.Debug('KeyInactivityTimeout set to ' + IntToStr(Value) + 'ms');
  223.   Form1.FKeyInactivityTimeout := Value;
  224. end;
  225.  
  226. procedure TMobileAgentApp.Transmit(const Cmd: WideString);
  227. begin
  228.   Form1.ScheduleTxAndWait(Cmd);
  229. end;
  230.  
  231. function TMobileAgentApp.Get_Connected: Integer;
  232. begin
  233.   if Form1.FConnected then Result := 1
  234.   else Result := 0;
  235. end;
  236.  
  237. function TMobileAgentApp.Get_LookupByNumber(
  238.   const Number: WideString): WideString;
  239. begin
  240.   Result := Form1.LookupContact(Number);
  241. end;
  242.  
  243. function TMobileAgentApp.Get_Received: WideString;
  244. begin
  245.   Result := Form1.FRxBuffer.Text;
  246. end;
  247.  
  248. procedure TMobileAgentApp.SentMessage(const Msg, DestNo: WideString;
  249.   ReqReply, Flash, StatusReq: Smallint);
  250. var
  251.   AReqReply, AFlash, AStatusReq: Boolean;
  252. begin
  253.   if ReqReply = 0 then AReqReply := False
  254.   else AReqReply := True;
  255.  
  256.   if Flash = 0 then AFlash := False
  257.   else AFlash := True;
  258.  
  259.   if StatusReq = 0 then AStatusReq := False
  260.   else AStatusReq := True;
  261.  
  262.   Form1.SentMessage('', Msg, DestNo, AReqReply, AFlash, AStatusReq);
  263. end;
  264.  
  265. procedure TMobileAgentApp.ObexPut(const filename: WideString);
  266. begin
  267.   Form1.ObexPutFile(filename);
  268. end;
  269.  
  270. procedure TMobileAgentApp.VoiceAnswer;
  271. begin
  272.   Form1.VoiceAnswer;
  273. end;
  274.  
  275. procedure TMobileAgentApp.VoiceCall(const Number: WideString);
  276. begin
  277.   Form1.VoiceCall(Number);
  278. end;
  279.  
  280. procedure TMobileAgentApp.VoiceHangUp;
  281. begin
  282.   Form1.VoiceHangUp;
  283. end;
  284.  
  285. function TMobileAgentApp.ObexGetObj(const filename,
  286.   objecturl: WideString): HResult;
  287. var
  288.   fs: TFileStream;
  289. begin
  290.   Result := 0; // error
  291.   try
  292.     fs := TFileStream.Create(filename,fmCreate);
  293.     try
  294.       with Form1 do begin
  295.         ObexConnect;
  296.         try
  297.           ObexGetObject(objecturl,TStream(fs));
  298.         finally
  299.           ObexDisconnect;
  300.         end;
  301.       end;
  302.       Result := 1;
  303.     finally
  304.       fs.Free;
  305.     end;
  306.   except
  307.   end;  
  308. end;
  309.  
  310. function TMobileAgentApp.ObexPutObj(const filename,
  311.   objecturl: WideString): HResult;
  312. var
  313.   fs: TFileStream;
  314. begin
  315.   Result := 0; // error
  316.   try
  317.     fs := TFileStream.Create(filename,fmOpenRead);
  318.     try
  319.       with Form1 do begin
  320.         ObexConnect;
  321.         try
  322.           ObexPutObject(objecturl,fs);
  323.         finally
  324.           ObexDisconnect;
  325.         end;
  326.       end;
  327.       Result := 1;
  328.     finally
  329.       fs.Free;
  330.     end;
  331.   except
  332.   end;  
  333. end;
  334.  
  335. function TMobileAgentApp.ObexGet(const filename,
  336.   objectname: WideString): HResult;
  337. begin
  338.   Result := 0; // error
  339.   try
  340.     Form1.ObexGetFile(filename,objectname,True);
  341.     Result := 1;
  342.   except
  343.   end;
  344. end;
  345.  
  346. procedure TMobileAgentApp.ObexDelete(const objectname: WideString);
  347. begin
  348.   Form1.ObexPutFile(objectname,True,True);
  349. end;
  350.  
  351. function TMobileAgentApp.ObexCut(const filename,
  352.   objectname: WideString): HResult;
  353. begin
  354.   with Form1 do begin
  355.     RequestConnection;
  356.     ObexConnect(ObexFolderBrowserServiceID);
  357.     try
  358.       FObex.GetFile(filename,objectname,True);
  359.       ObexPutFile(objectname,True,True);
  360.     finally
  361.       ObexDisconnect;
  362.     end;
  363.   end;
  364. end;
  365.  
  366. procedure TMobileAgentApp.AddTimer(msec: Integer; const event: WideString);
  367. var
  368.   timer: TTimer;
  369. begin
  370.   DeleteTimer(event);
  371.   timer := TTimer.Create(nil);
  372.   timer.OnTimer := TimerEvent;
  373.   timer.Interval := msec;
  374.   tl.AddObject(event,timer);
  375. end;
  376.  
  377. procedure TMobileAgentApp.TimerEvent(Sender: TObject);
  378. var
  379.   i: Integer;
  380. begin
  381.   for i := 0 to tl.Count-1 do
  382.     if tl.Objects[i] = Sender then
  383.     try
  384.       with Sender as TTimer do
  385.       try
  386.         Enabled := False;
  387.  
  388.         Form1.CallScriptMethod(tl[i], []);
  389.       finally
  390.         Enabled := True;
  391.       end;
  392.       break;
  393.     except
  394.       on e: Exception do Debug(e.Message);
  395.     end;
  396. end;
  397.  
  398. procedure TMobileAgentApp.DeleteTimer(const event: WideString);
  399. var
  400.   i: Integer;
  401. begin
  402.   for i := 0 to tl.Count-1 do
  403.     if WideCompareText(tl[i],event) = 0 then begin
  404.       with tl.Objects[i] as TTimer do begin
  405.         Enabled := False;
  406.         Free;
  407.       end;
  408.       tl.Delete(i);
  409.       break;
  410.     end;
  411. end;
  412.  
  413. destructor TMobileAgentApp.Destroy;
  414. var
  415.   i: Integer;
  416. begin
  417.   sl.Free;
  418.   tl.Free;
  419.  
  420.   { Remove Timer events }
  421.   for i := tl.Count-1 downto 0 do DeleteTimer(tl[i]);
  422.   { Remove Main menu entries }
  423.   for i := rootMenu.Count-1 downto rootCount do rootMenu.Delete(i);
  424.   inherited;
  425. end;
  426.  
  427. procedure TMobileAgentApp.DisableKeyMonitor;
  428. begin
  429.   Form1.DisableKeyMonitor(True);
  430. end;
  431.  
  432. procedure TMobileAgentApp.ScriptCall(const Code: WideString);
  433. begin
  434.   Form1.ScriptControl.CallFunction(__fma_objcall,[Code]);
  435. end;
  436.  
  437. procedure TMobileAgentApp.Sleep(msec: Integer);
  438. begin
  439.   WaitASec(msec,True);
  440. end;
  441.  
  442. function TMobileAgentApp.Get_PhoneType(
  443.   const Number: WideString): WideString;
  444. var
  445.   contact: PContactData;
  446.   FullName: WideString;
  447. begin
  448.   Result := ''; // unknown
  449.   if Form1.FUseObex and not Form1.FStartupOptions.NoIRMC then begin
  450.     FullName := Form1.LookupContact(Number);
  451.     if (FullName <> '') and Form1.frmSyncPhonebook.FindContact(FullName,contact) then
  452.       Result := GetContactPhoneType(contact,Number);
  453.   end;
  454. end;
  455.  
  456. procedure TMobileAgentApp.DisconnectTemporary;
  457. begin
  458.   Form1.DoDisconnectTemporary;
  459. end;
  460.  
  461. initialization
  462.   rootCount := 0;
  463.   TAutoObjectFactory.Create(ComServer, TMobileAgentApp, Class_MobileAgentApp,
  464.     ciMultiInstance, tmSingle);
  465. end.
  466.